package org.concepts.java.antlr.validator;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTree;
import org.concepts.java.antlr.utils.AntlrUtils;
import org.junit.Test;

public class TestValParser {

  @Test
  public void testParserOneRecord() throws RecognitionException {
    processInputUsingValParser("name = Foo;");
  }

  /**
   * Process the input using {@link ValParser}
   * 
   * @param input
   *          input. Cannot be null.
   * @throws RecognitionException
   *           Thrown if tokens don't follow the rule
   */
  private void processInputUsingValParser(String input)
      throws RecognitionException {
    CommonTree tree = (CommonTree) getValParser(input).variableDeclaration().tree;
    System.out.println("\n\n" + "AST text : " + tree.getText() + "\n"
        + "AST tree : " + tree.toStringTree());
  }

  /**
   * Returns an instance of Validator parser for the input
   * 
   * @param input
   *          input. Cannot be null.
   * @return val parser. Is never null.
   */
  private ValParser getValParser(String input) {
    System.out.println("\n\nInput [" + input + "]\n\nLexer tokens:\n");
    AntlrUtils
        .processAndPrintTokens(new ValLexer(new ANTLRStringStream(input)));
    ValLexer valLexer = new ValLexer(new ANTLRStringStream(input));
    ValParser valParser = new ValParser(new CommonTokenStream(valLexer));
    return valParser;
  }

}
